package com.syn.michat.util;

import com.alibaba.fastjson.JSON;
import com.syn.michat.constant.ErrorContants;
import com.syn.michat.model.ErrorResponseEntity;
import com.syn.michat.model.ResponseEntity;
import com.syn.michat.util.exception.ClientException;

/**
 * 错误工具类
 **/

public class ResponseErrorUtil {

    /**
    * @Description: 将ClientException转为ResponseEntity
    * @Params: [e]
    * @return: com.syn.michat.model.ResponseEntity
    * @Author: 申铭
    * @Date: 2021/12/14
    */
    static public ResponseEntity convertException(ClientException e) {
        ErrorResponseEntity entity = new ErrorResponseEntity();
        entity.setStatus(ErrorContants.RESPONSE_STATUS_FAILURE);
        entity.setCode(e.getCode());
        entity.setMessage(e.getMessage());
        entity.setErrorData(JSON.toJSONString(e.getData()));
        return entity;
    }

    static public ResponseEntity convertException(Exception e) {
        ErrorResponseEntity entity = new ErrorResponseEntity();
        entity.setStatus(ErrorContants.RESPONSE_STATUS_FAILURE);
        entity.setCode(ErrorContants.UNKNOWN);
        entity.setMessage(e.getMessage());
        return entity;
    }
}
